/*
 * @Description:
 * @Author: fhw
 * @Date: 2023-02-17 13:58:56
 */
/*
 * @Description:
 * @Author: fhw
 * @Date: 2023-02-17 13:58:56
 */
import { ApiProperty } from '@nestjs/swagger';
import { Exclude } from 'class-transformer';
import {
  Column,
  Entity,
  ManyToOne,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  BeforeInsert,
  OneToMany,
  JoinColumn,
} from 'typeorm';
import { hashSync } from 'bcryptjs';
import { RoleEntity } from 'src/modules/role/entities/role.entity';
import { ArticleList } from 'src/modules/article/entities/article.entity';

@Entity({ name: 'admin-user' })
export class AdminUser {
  @PrimaryGeneratedColumn()
  id: number;
  /** 用户名称 */
  @ApiProperty({ example: 'username', description: '用户名称' })
  @Column({
    type: 'varchar',
    length: 30,
    nullable: false,
    comment: '用户名称',
  })
  username: string;
  /** 账号 */
  @ApiProperty({ example: 'account ', description: '账号' })
  @Column({
    type: 'varchar',
    length: 100,
    comment: '账号',
    unique: true,
  })
  account: string;
  /** 用户头像 */
  @ApiProperty({ example: 'avatar', description: '用户头像' })
  @Column({
    type: 'text',
    comment: '用户头像',
    nullable: true,
  })
  avatar: string;
  /** 密码 */
  @ApiProperty({ example: 'password', description: '密码' })
  @Exclude()
  @Column({
    type: 'varchar',
    length: 100,
    comment: '密码',
    select: false,
  })
  password: string;

  @BeforeInsert()
  async encryptPwd() {
    this.password = await hashSync(this.password);
  }
  /** 是否启用：0否，1是 */
  @ApiProperty({ example: 'del', description: '是否启用：0否，1是' })
  @Column({
    type: 'enum',
    enum: [0, 1],
    comment: '是否启用：0否，1是',
    default: 0,
  })
  isDel: string;

  /** 创建时间 */
  @ApiProperty({ example: 'createDate', description: '创建时间' })
  @CreateDateColumn({
    type: 'datetime',
    comment: '创建时间',
  })
  createDate: string;

  /** 更新时间 */
  @ApiProperty({ example: 'updateDate', description: '更新时间' })
  @UpdateDateColumn({
    type: 'datetime',
    comment: '更新时间',
  })
  updateDate: string;

  /** 角色Id */
  @ApiProperty({ example: 'rolesId', description: '角色Id' })
  @Column({
    type: 'int',
    comment: '角色Id',
    nullable: true
  })
  rolesId: number;
  @ManyToOne(() => RoleEntity, (role) => role.id)
  @JoinColumn({ name: 'rolesId' })
  roles: RoleEntity;
  // 角色创建人
  @OneToMany(() => RoleEntity, (role) => role.createBy)
  rolesCreateBy: RoleEntity;
  // 文章创建人
  @OneToMany(() => ArticleList, (role) => role.createBy)
  articleCreateBy: ArticleList;
}
